package com.ruoyi.framework.mybatisplus.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * @author yql
 * @describe ControllerSqlQueueUtils
 * @date 2023/3/9 9:47
 */

public class ControllerSqlQueueUtils {
    private static final ThreadLocal<ArrayBlockingQueue<String>> threadLocal = new ThreadLocal<>();

    public static ArrayBlockingQueue<String> getQueue() {
        ArrayBlockingQueue<String> queue = threadLocal.get();
        if (queue == null) {
            threadLocal.set(new ArrayBlockingQueue<>(100));
            return getQueue();
        }
        return queue;
    }

    public static void appendSql(String sql) {
        if (!sql.endsWith(";")) {
            sql = sql + ";";
        }
        getQueue().add(sql);
    }

    public static List<String> getSqlList() {
        return new ArrayList<>(getQueue());
    }

    public static void clear() {
        threadLocal.remove();
    }

}
